function getCookie(name) {
    var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return r ? r[1] : undefined;
}

$(document).ready(function() {
    // $('.popup_con').fadeIn('fast');
    // $('.popup_con').fadeOut('fast');
    // 检测是否登录
    $.get('/api/user/check_login',{},function (xhr) {
        if(xhr.errno != 0){
            alert('您尚未登录');
            window.location.href = './login.html';
        }
    });
    //获取城区信息
    $.get("/api/house/areas", function (xhr) {
        if (xhr.errno == 0) {
            //使用前端模板渲染页面
            area_html = template('area_tpl', {areas: JSON.parse(xhr.data.areas)})
            $('#area-id').html(area_html)
        } else {
            alert(xhr.errmsg);
        }
    });
    //获取配套设施信息
    $.get("/api/house/facility", function (xhr) {
        if (xhr.errno == 0) {
            //使用前端模板渲染页面
            facility_html = template('facility_tpl', {facility_info: JSON.parse(xhr.data.facility_info)})
            $('#house_facility').html(facility_html)
        } else {
            alert(xhr.errmsg);
        }
    });

    //房源基本信息表单提交
    $('#form-house-info').submit(function (event) {
        event.preventDefault();
        //获取表单数据，转换为json发送到后端
        var houseData = {};
        $(this).serializeArray().map(function (x) {
            houseData[x.name] = x.value;
        });
        //设施信息
        var facilities = []
        $(':checked[name=facility]').each(function (index, x) {
            facilities[index] = $(x).val();
        });
        houseData.facility = facilities;
        $.ajax({
            url: '/api/house/info',
            type: 'post',
            data: JSON.stringify(houseData),
            dataType: 'json',
            contentType: 'application/json',
            headers:{
                'X-CSRFToken':getCookie('csrf_token')
            },
            success: function (xhr) {
                if (xhr.errno == 4101) {
                    // 用户未登录
                    location.href = "/login.html";
                } else if (xhr.errno == 0) {
                    // 保存成功
                    // 隐藏基本信息表单
                    $("#form-house-info").hide();
                    // 显示图片表单
                    $("#form-house-image").show();
                    // 设置图片表单中的房屋id
                    $("#house-id").val(xhr.data.house_id);
                } else {
                    alert(xhr.errmsg);
                }
            }
        });
    });

    //房源图片上传
    $('#form-house-image').submit(function (event) {
        //阻止表单默认行为
        event.preventDefault();
        //使用插件构造ajax请求
        $(this).ajaxSubmit({
            url: '/api/house/image',
            type: 'post',
            dataType: 'json',
            headers: {
                'X-CSRFToken': getCookie('csrf_token')
            },
            success: function (xhr) {
                if (xhr.errno == 0){
                    // 保存图片成功
                    $(".house-image-cons").append('<img src="'+ xhr.data.image_url +'">');
                } else if (xhr.errno == 4101) {
                    location.href = "/login.html";
                } else {
                    alert(xhr.errmsg);
                }
            }
        });

    });
});
